package com.pb.b2b.model.enums;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.pb.b2b.common.enums.EnumarationType;
import com.pb.b2b.util.FacesUtils;

public class Status extends EnumarationType {
	private static final long serialVersionUID = 1L;

	public static final Status PASSIVE = new Status(new Integer(0), "PASSIVE");
	public static final Status AKTIVE = new Status(new Integer(1), "AKTIVE");

	protected static ArrayList<Status> list = new ArrayList<Status>();

	static {
		// Only Types.VARCHAR and Types.INTEGER can be used!
		SQL_TYPES = new int[] { Types.INTEGER };
		list.add(PASSIVE);
		list.add(AKTIVE);
	}

	public Status() {
	}

	// if the method name starts with 'get' than it causes recursion
	// on hibernate usertype mapping when parsion to json.
	// So we change it to 'fetch'
	public List<Status> fetchList() {
		return list;
	}

	public Status(Integer value, String label) {
		super(value, label);
	}

	public static Status getById(int id) {
		Iterator<Status> it = list.iterator();
		while (it.hasNext()) {
			Status status = it.next();
			int statusId = Integer.parseInt(status.getValue());
			if (statusId == id)
				return status;
		}
		return null;
	}

	@Override
	public String toString() {
		String bundleName = "resources.application";
		String tmp = FacesUtils.getResourceBundleString(bundleName, "lbl_status_" + this.value, null);
		return tmp;
	}
}
